_realname=octave
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=7.3.0
pkgrel=6
pkgdesc="GNU Octave: Interactive programming environment for numerical computations (mingw-w64)"
url="https://www.octave.org"
license=('spdx:GPL-3.0-or-later')
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
         "${MINGW_PACKAGE_PREFIX}-lapack"
         "${MINGW_PACKAGE_PREFIX}-arpack"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-ghostscript"
         "${MINGW_PACKAGE_PREFIX}-gl2ps"
         "${MINGW_PACKAGE_PREFIX}-glpk"
         "${MINGW_PACKAGE_PREFIX}-graphicsmagick"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-libsndfile"
         "${MINGW_PACKAGE_PREFIX}-pcre"
         "${MINGW_PACKAGE_PREFIX}-qhull"
         $([[ ${CARCH} == i686 ]] && echo "${MINGW_PACKAGE_PREFIX}-suitesparse5" || echo "${MINGW_PACKAGE_PREFIX}-suitesparse")
         "${MINGW_PACKAGE_PREFIX}-sundials"
         "${MINGW_PACKAGE_PREFIX}-qrupdate"
         "${MINGW_PACKAGE_PREFIX}-qscintilla"
         "${MINGW_PACKAGE_PREFIX}-qt5-tools")
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-gnuplot"
             "${MINGW_PACKAGE_PREFIX}-gperf"
             "${MINGW_PACKAGE_PREFIX}-fltk"
             "${MINGW_PACKAGE_PREFIX}-portaudio"
             "${MINGW_PACKAGE_PREFIX}-rapidjson")
optdepends=('texinfo: for help-support in Octave'
            "${MINGW_PACKAGE_PREFIX}-epstool: eps output with tight bounding box"
            "${MINGW_PACKAGE_PREFIX}-fltk: alternative plotting and dialogs"
            "${MINGW_PACKAGE_PREFIX}-gnuplot: alternative plotting"
            "${MINGW_PACKAGE_PREFIX}-portaudio: audio support")
source=(https://ftp.gnu.org/gnu/octave/octave-$pkgver.tar.xz{,.sig}
        "0001-console-codepage.patch"  # see: https://hg.savannah.gnu.org/hgweb/octave/rev/ea0b06534a37
        "0002-mk-doc-cache-path.patch"
        "0005-makeinfo-perl.patch")
validpgpkeys=('DBD9C84E39FE1AAE99F04446B05F05B75D36644B')  # John W. Eaton
sha512sums=('1399d7010cbd4e83d6dc60ab8cc2abb20e64a762e905a00066e6331c5350df93b4ff8f2670fd52fc183918cea21bc176cb263ca71080de6e4041f5e0ea21558e'
            'SKIP'
            '3d840246486fabc02d01eb40a742e8e4b050b73426308f09c986779054d045868df7b943e4057e86c3cdfc3157915548f4cf336b143aac632253053d1bec3129'
            '5742cde34ae9184276c830dab41dccb289991578656353658bf07315d89b1c3870c6c51520d5aae0cec8c7fb12c707a1970aa9dde353b4ab54e515d1d3cf66e0'
            'c2796f1d83208595c4f52f27d14f3bedc64d95a514089108726b5a2de20296bdfb195f545e7045a322eebef4ce387b0f05547e071c74dfa5478f5689d0f207bd')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd "${_realname}-${pkgver}"

  apply_patch_with_msg \
    0001-console-codepage.patch \
    0002-mk-doc-cache-path.patch \
    0005-makeinfo-perl.patch
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  mkdir -p build-${MSYSTEM}
  cd build-${MSYSTEM}

  # The configure step sometimes hangs for MINGW32 for currently unknown reasons.
  # Add a timeout of 30 minutes (well over what should be needed) to unblock
  # the build system in that case.
  timeout 30m \
  ../${_realname}-${pkgver}/configure \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --libexecdir=${MINGW_PREFIX}/lib \
    --sbindir=${MINGW_PREFIX}/bin \
    --enable-shared \
    --disable-static \
    --enable-relocate-all \
    --enable-link-all-dependencies \
    ac_cv_search_tputs=-ltermcap \
    JAVA_HOME=""

  PERL5SHELL="bash -l -c" make all
}

check() {
  cd "${srcdir}"/build-${MSYSTEM}
  make check || true
}

package() {
  make -C "build-${MSYSTEM}" DESTDIR="${pkgdir}" install

  # Remove full path reference
  local _PREFIX_WIN="$(cygpath -wm ${MINGW_PREFIX})"
  for pcfile in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${_PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pcfile}"
  done
}
